package com.blankm.codec_tools;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ResourceUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankm.codec_tools.databinding.ActivityPlayerBinding;


public class PlayerActivity extends AppCompatActivity
        implements View.OnClickListener {


    ActivityPlayerBinding binding;


    private final String TAG = PlayerActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_player);
        //复制视频
        copyVideo();
        binding.btnPlay.setOnClickListener(this);
        binding.btnStop.setOnClickListener(this);
        VideoLogUtils.setIsLog(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_play:
                playerVideo();
                break;
            case R.id.btn_stop:
                binding.videoPlayer.release();
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.videoPlayer.release();
    }

    @Override
    public void onBackPressed() {
        binding.videoPlayer.release();
        super.onBackPressed();
    }

    private void copyVideo() {
        ThreadUtils.executeByIo(new ThreadUtils.SimpleTask<Boolean>() {
            @Override
            public Boolean doInBackground() throws Throwable {
                String dir = "sdcard/codec_tools";
                if (FileUtils.isFile(dir + "/test_3.ts")
                        && FileUtils.isFile(dir + "/test_4.ts")) {
                    LogUtils.eTag(TAG, "文件存在");
                    return true;
                }

                if (FileUtils.isDir(dir)) {
                    LogUtils.eTag(TAG, "文件夹存在");
                } else {
                    LogUtils.eTag(TAG, "文件夹不存在");
                    boolean a = FileUtils.createOrExistsDir(dir);
                    LogUtils.eTag(TAG, "文件夹创建" + (a ? "成功" : "失败"));
                }
                return ResourceUtils.copyFileFromAssets("video", dir);
            }

            @Override
            public void onSuccess(Boolean result) {
                LogUtils.eTag(TAG, result ? "文件复制成功" : "文件复制失败");
            }
        });
    }


    private void playerVideo() {
        try {
//            binding.videoPlayer.setUrl("sdcard/codec_tools/test_4.ts");
            binding.videoPlayer.setUrl("http://172.31.243.146:9999/static/aiarea/rtmp/DH20220608000048.ts");
//            binding.videoPlayer.setLooping(true);
            binding.videoPlayer.start();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }


}